package com.example.chao.myphotosearch.config;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.chao.myphotosearch.bean.ImageJoy;
import com.example.chao.myphotosearch.bean.DownloadImg;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by chao on 2016/4/29.
 */
public class DBManger {
    private DBHelper dbHelper;
    private SQLiteDatabase database;
    public DBManger(Context context) {
         dbHelper = new DBHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    public void addHasDownLoad(DownloadImg downloadImg){
        database.beginTransaction();
        database.execSQL("insert into " + MySql.DOWN_LOAD_TABLE + " values(null,?,?,?,?)",
                new Object[]{downloadImg.getName(), downloadImg.getLargUrl(), downloadImg.getHeight(), downloadImg.getWidth()});
        database.setTransactionSuccessful();
        database.endTransaction();
    }

    public boolean isExisted(String downloadImgUrl,String tableName){
        Cursor cursor = database.rawQuery("select * from "+tableName+" where largeImgUrl='"+downloadImgUrl+"'",null);
        if(cursor.getCount()!=0){
            return true;
        }else {
            return false;
        }
    }

    public ArrayList<DownloadImg> getDownLoadImgs(){
        ArrayList<DownloadImg> imgs = new ArrayList<>();
        Cursor cursor = database.rawQuery("select * from "+MySql.DOWN_LOAD_TABLE+" order by id desc",null);
        if(cursor.getCount()!=0&&cursor.moveToFirst()){
            do{
                DownloadImg downloadImg = new DownloadImg();
                downloadImg.setName(cursor.getString(cursor.getColumnIndex("fileName")));
                downloadImg.setLargUrl(cursor.getString(cursor.getColumnIndex("largeImgUrl")));
                downloadImg.setWidth(cursor.getInt(cursor.getColumnIndex("width")));
                downloadImg.setHeight(cursor.getInt(cursor.getColumnIndex("height")));
                imgs.add(downloadImg);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return imgs;
    }

    /**
     * 将已删除的下载图片信息从数据库里删除
     */
    public void deleteHasDownload(String fileName){
        database.beginTransaction();
        database.execSQL("delete from " + MySql.DOWN_LOAD_TABLE + " where fileName =?", new Object[]{fileName});
        //  db.execSQL("delete from " + MySql.DownloadTable + " where fileName = " + "'" + fileName + "'");
        database.setTransactionSuccessful();
        database.endTransaction();
    }
    /**
     * 批量删除已选中下载的图片
     */
    public void deleteDownloadPictures(ArrayList<DownloadImg> imgs){
        database.beginTransaction();
        for(DownloadImg img :imgs){
            new File(img.getName()).delete();
            database.execSQL("delete from "+MySql.DOWN_LOAD_TABLE+" where fileName =?",new Object[]{img.getName()});
        }

        database.setTransactionSuccessful();
        database.endTransaction();
    }
    /**
     * 添加已收藏图片信息到数据库
     */
    public void addHasCollect(ImageJoy img){
        database.beginTransaction();
        database.execSQL("insert into " + MySql.COLLECT_TABLE + " values(null,?,?,?,?)",
                new Object[]{img.getThumburl(), img.getSourceurl(), img.getHeight(), img.getWidth()});
        database.setTransactionSuccessful();
        database.endTransaction();
    }
    /**
     * 将已删除的收藏图片信息从数据库里删除
     */
    public void deleteHasCollect(String largeImgUrl){
        database.beginTransaction();
        database.execSQL("delete from "+MySql.COLLECT_TABLE+" where largeImgUrl = ?",new Object[]{largeImgUrl});
        database.setTransactionSuccessful();
        database.endTransaction();
    }

    /**
     * 批量删除已选中下载的图片
     */
    public void deleteCollectPictures(ArrayList<ImageJoy> imgs){
        database.beginTransaction();
        for(ImageJoy img :imgs){
            database.execSQL("delete from "+MySql.COLLECT_TABLE+" where largeImgUrl =?",new Object[]{img.getThumburl()});
        }

        database.setTransactionSuccessful();
        database.endTransaction();
    }
    /**
     * 批量删除已选中下载的图片
     */
    public void deleteDownLoadPictures(ArrayList<DownloadImg> imgs){
        database.beginTransaction();
        for(DownloadImg img :imgs){
            database.execSQL("delete from "+MySql.DOWN_LOAD_TABLE+" where largeImgUrl =?",new Object[]{img.getLargUrl()});
        }

        database.setTransactionSuccessful();
        database.endTransaction();
    }
    /**
     * 查询所有收藏的图片信息
     * @return
     */
    public ArrayList<ImageJoy> queryHasCollectImgs(){
        ArrayList<ImageJoy> imgs = new ArrayList<ImageJoy>();
        Cursor cursor = database.rawQuery("select * from "+MySql.COLLECT_TABLE+" order by id desc",null);

        if (cursor!=null&&cursor.moveToFirst()){
            do{
                ImageJoy joy= new ImageJoy();
                joy.setSourceurl(cursor.getString(cursor.getColumnIndex("smallImgUrl")));
                joy.setThumburl(cursor.getString(cursor.getColumnIndex("largeImgUrl")));
                joy.setHeight(cursor.getInt(cursor.getColumnIndex("height")));
                joy.setWidth(cursor.getInt(cursor.getColumnIndex("width")));
                imgs.add(joy);
            }while(cursor.moveToNext());
        }
        cursor.close();
        return imgs;

    }
}
